//
//  PlayerViewController.m
//  YUXiu
//
//  Created by wang on 16/6/27.
//  Copyright © 2016年 Wang. All rights reserved.
//

#import "PlayerViewController.h"
#import "KRVideoPlayerController.h"
#import "PlayerCell.h"
#import "Cusbtn.h"
#import "PayViewController.h"


@interface PlayerViewController (){
    NSInteger num;
    NSArray *urlArray;
    NSArray *titleArray;
    UIView *vipView;
    UIButton *vipTopup;
    UILabel *contentLabel;
    UITableView *mainTable;
    UIView *topView;
    UIView *infoView;//课程介绍view
    NSArray *dataFalse;
    BOOL infoShow;
}

@property (nonatomic, strong) KRVideoPlayerController *videoController;


@end

@implementation PlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    num = -1;
    self.title = @"课程详情";
    [self initNavi];
}
#pragma mark - UI
-(void)autoConfigViews{
    [self.tableView setHidden:YES];
    [self.navigationController.navigationBar setTranslucent:YES];
    [self creatTopView];
    urlArray = @[@"http://krtv.qiniudn.com/150522nextapp",@"http://11945.long-vod.cdn.aodianyun.com/u/11945/m3u8/adaptive/155a4da63c32415cf7b6441d70bd77be.m3u8",@"http://krtv.qiniudn.com/150522nextapp",@"http://11945.long-vod.cdn.aodianyun.com/u/11945/m3u8/adaptive/155a4da63c32415cf7b6441d70bd77be.m3u8",@"http://krtv.qiniudn.com/150522nextapp",@"http://11945.long-vod.cdn.aodianyun.com/u/11945/m3u8/adaptive/155a4da63c32415cf7b6441d70bd77be.m3u8"];
    titleArray =@[@"下一个应用",@"瑜秀。。。。。",@"下一个应用",@"瑜秀。。。。。",@"下一个应用",@"瑜秀。。。。。"];

    mainTable = [[UITableView alloc]initWithFrame:krect(0, kScreenWidth*9/16+64+40, kScreenWidth, kScreenHeight-kScreenWidth*9/16-104) style:UITableViewStylePlain];
    mainTable.delegate = self;
    mainTable.dataSource = self;
    mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:mainTable];
    
    if (_isVIP) {
        vipView = [[UIView alloc]initWithFrame:krect(0, kScreenHeight-50, kScreenWidth, 50)];
        vipView.backgroundColor = kNaviBarCOLOR;
       contentLabel = [[UILabel alloc]initWithFrame:krect(0, 0, kScreenWidth*3/5, 50)];
        contentLabel.text = @"你的会员还剩下20天";
        contentLabel.textAlignment = NSTextAlignmentCenter;
        [vipView addSubview:contentLabel];
        
        vipTopup = [[UIButton alloc]initWithFrame:krect(kScreenWidth*3/5, 0, kScreenWidth*2/5, 50)];
        vipTopup.backgroundColor = kOrangeCOLOR;
        [vipTopup setTitle:@"续费" forState:UIControlStateNormal];
        [vipView addSubview:vipTopup];
        
        [self.view insertSubview:vipView aboveSubview:mainTable];
        [mainTable setFrame:krect(0, kScreenWidth*9/16+64+40, kScreenWidth, kScreenHeight-kScreenWidth*9/16-104-50)];
    }else{
        [mainTable setFrame:krect(0, kScreenWidth*9/16+64+40, kScreenWidth, kScreenHeight-kScreenWidth*9/16-104)];
    }
    
}
-(void)creatTopView{
    topView = [[UIView alloc]initWithFrame:krect(0, 64, kScreenWidth, kScreenWidth*9/16 + 40)];
    topView.backgroundColor = kWhiteCOLOR;
    UIView *sectionV = [self getHeaderView];
    
    [topView addSubview:sectionV];
    [self.view addSubview:topView];
    
}
-(UIView *)getHeaderView{
    UIView *hdView = [[UIView alloc]initWithFrame:krect(0, kScreenWidth*9/16, kScreenWidth, 40)];
    UIView *line = [[UIView alloc]initWithFrame:krect(0, 0, 3, 40)];
    line.backgroundColor = kOrangeCOLOR;
    [hdView addSubview:line];
    
    //课程合集名称
    UILabel * allSetName = [[UILabel alloc]initWithFrame:krect(15, 10, kScreenWidth/2, 20)];
    allSetName.font = Font(14);
    allSetName.text = @"课程合集名称";
    [hdView addSubview:allSetName];
    
    //课程介绍
    UIButton *introuButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-100, 0, 80, 40)];
    UILabel *tiLabel = [UILabel new];
    tiLabel.textColor = kOrangeCOLOR;
    introuButton.tag = 999;
    tiLabel.text = @"课程介绍";
    tiLabel.textAlignment = NSTextAlignmentLeft;
    tiLabel.font = FontB(14);
    tiLabel.adjustsFontSizeToFitWidth = YES;
    [introuButton addSubview:tiLabel];
    
    [tiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.edges.equalTo(_cityButton).with.insets(UIEdgeInsetsMake(0, 0, 0, 20));
        make.left.equalTo(introuButton.mas_left).with.offset(0);
        make.top.equalTo(introuButton.mas_top).with.offset(0);
        make.width.equalTo(@70);
        make.bottom.equalTo(introuButton.mas_bottom).with.offset(0);
        
    }];
    
    UIImageView *img = [UIImageView new];
    [img setImage:kCreatImage(@"header")];
    [introuButton addSubview:img];
    
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(introuButton.mas_right).with.offset(0);
        make.left.equalTo(tiLabel.mas_right).with.offset(0);
        make.width.equalTo(@20);
        make.top.equalTo(introuButton.mas_top).with.offset(10);
        make.bottom.equalTo(introuButton.mas_bottom).with.offset(-10);
    }];
    
    
    [introuButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [hdView addSubview:introuButton];
    
    UIView *bottomline = [[UIView alloc]initWithFrame:krect(2, 39, kScreenWidth-4, 1)];
    bottomline.backgroundColor = kNaviBarCOLOR;
    [hdView addSubview:bottomline];
    return hdView;
}
-(void)initNavi{
    //test use
    UIButton *right = [[UIButton alloc]initWithFrame:krect(kScreenWidth-80, 30, 60, 20)];
    [right setTitle:@"跳转支付" forState:UIControlStateNormal];
    [right setTitleColor:kOrangeCOLOR forState:UIControlStateNormal];
    [right.titleLabel setFont:Font(12)];
    
    [right addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    right.tag = 998;
    [self.navigationController.view addSubview:right];
//    self.navigationController.navigationItem.rightBarButtonItem = rItem;
    
}
#pragma mark - tableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   static NSString *identity = @"cell";
    PlayerCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
    if (!cell) {
        cell = [[PlayerCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identity];
       
    }
    cell.titleL.text = [NSString stringWithFormat:@"第%ld天 %@",(long)indexPath.row+1,titleArray[indexPath.row]];
    cell.desLabel.text = @"练习时长:30分钟";
    if (indexPath.row == num) {
        cell.playingL.text = @"正在播放...";
        [cell.playButton setBackgroundImage:[UIImage imageNamed:@"navigationbar_Personal_normal"] forState:UIControlStateNormal];

    }else{
        cell.playingL.text = @"";
        [cell.playButton setBackgroundImage:[UIImage imageNamed:@"header"] forState:UIControlStateNormal];

    }
    cell.block = ^{
        //点击某个item，添加播放。
        NSURL *videoURL = [NSURL URLWithString:urlArray[indexPath.row]];
       
        [self playVideoWithURL:videoURL];
        [mainTable reloadData];
        num = indexPath.row;
        
    };
    return cell;
}
#pragma mark - TouchEvent
-(void)click:(UIButton *)btn{
    if (btn.tag == 999) {
        //课程介绍view
        if (!infoShow) {
            if (!infoView) {
                infoView = [[UIView alloc]initWithFrame:mainTable.frame];
                UITextView *teView = [[UITextView alloc]initWithFrame:krect(0, 0, infoView.golf_width, infoView.golf_height)];
                teView.font = Font(12);
                teView.editable = NO;
                teView.text = @"瑜伽的起源\n瑜伽起源于印度，流行于世界。 瑜珈一词原初的意思是驾驭牛马，从遥远的古代起她代表设想帮助达到最高目的的某些实践或是修炼。在古圣贤帕坦珈利所著的《瑜珈经》中，准确的定义'对心作用的控制'。 瑜珈在印度有着渊远的历史。在我们所知道的古印度婆罗门体系中与其有着密切的关系， 在印度，人们相信通过瑜珈可以摆脱轮回的痛苦，内在的自我将与宇宙的无上我合一；通过瑜珈将产生轮回的种子烧毁，心的主体被证悟，一切障碍都将不存在。 在印度现在很难区分瑜珈与印度教的关系，在寺庙中、在经典中、在生活中、在许许多多的范围，两者的关系都彼此相互融合。 瑜伽是东方最古老的强身术之一。它产生于公元前，是人类智慧的结晶。瑜伽也是印度先贤在最深沉的观想和静定状态下，从直觉了悟生命的认知。瑜伽修持秘要是理论和实践互相参证的法典。";
                [infoView addSubview:teView];
                [self.view addSubview:infoView];
                
            }else{
                [infoView setHidden:NO];
            }
            [mainTable setHidden:YES];

        }else{
            [mainTable setHidden:NO];
            [infoView setHidden:YES];
        }
        
        infoShow = !infoShow;
    }else{
        //998 right item,跳支付
        PayViewController *PayVC = [[PayViewController alloc]init];
        [self.navigationController pushViewController:PayVC animated:YES];

    }
}

//添加播放器
- (void)playVideoWithURL:(NSURL *)url
{
    if (!self.videoController) {
        self.videoController = [[KRVideoPlayerController alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenWidth*(9.0/16.0))];
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
        [self.videoController showInWindow];
    }
    self.videoController.contentURL = url;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.videoController dismiss];
    UIButton *btn = [self.navigationController.view viewWithTag:998];
    [btn setHidden:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIButton *btn = [self.navigationController.view viewWithTag:998];
    if (btn) {
        [btn setHidden:NO];
    }
    [self playVideoWithURL:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
